package com.settlement.system.common.util.common;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 获取FAS数据字典
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class DictionaryUtil {

    private final RedisTemplate redisTemplate;

    private static final String FAS_SYS_DICT = "fas_sys_dict";

    private static final String DICT_VALUE = "dictValue";

    private static final String DICT_LABEL = "dictLabel";

    private static final String ACS_BRAND = "ACS_BRAND";

    private static final String ACS_TO_BE_DELETED_SCHEDULE_ID = "ACS_scheduleId";

    public static void main(String[] args) {

    }

    public class BaseData {
        /**
         * value
         */
        private Object value;
        /**
         * text
         */
        private String text;

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    }

    /**
     * 根据传进来的标签迭代取出相应的MAP
     * @param item
     * @param dictLabelParam
     * @return
     */
    public Map<String, Object> getDictionaryBykey(String item,String dictLabelParam) {
        return getDictionaryBykey(null, item, dictLabelParam);
    }

    /**
     * 根据传进来的标签迭代取出相应的MAP
     * @param item
     * @param dictLabelParam
     * @return
     */
    public Map<String, Object> getDictionaryBykey(String dictKey, String item,String dictLabelParam) {
        try {
            if (StringUtils.isBlank(dictKey)) {
                dictKey = FAS_SYS_DICT;
            }
            //Object hget = RedisUtil.hget(dictKey, item);
            String jsonStr = (String) redisTemplate.opsForHash().get(dictKey, item);

            if (StringUtils.isEmpty(jsonStr)) {
                return Collections.emptyMap();
            }
            List<Map<String, Object>> lists = JSON.parseObject(jsonStr, List.class);
            return lists.stream().filter(Objects::nonNull)
                    .filter(key -> key.containsValue(dictLabelParam))
                    .findAny()
                    .orElseGet(Collections::emptyMap);
        } catch (Exception e) {
            log.error("取数据字典异常", e);
            return Collections.emptyMap();
        }
    }

    /**
     * 按基本数据类型获取字典
     *
     * @param dictKey  key
     * @param dictType 类型
     * @return 按基本数据类型获取字典
     */
    public List<BaseData> getDictionaryByType(String dictKey, String dictType) {
        try {
            if (StringUtils.isBlank(dictKey)) {
                dictKey = FAS_SYS_DICT;
            }
            String jsonStr = (String)redisTemplate.opsForHash().get(dictKey, dictType);
            if (StringUtils.isEmpty(jsonStr)) {
                return Lists.newArrayListWithExpectedSize(0);
            }
            List<Map<String, Object>> lists = JSON.parseObject(jsonStr, List.class);
            List<BaseData> baseDataList = Lists.newArrayListWithExpectedSize(lists.size());
            for (Map<String, Object> map : lists) {
                BaseData baseData = new BaseData();
                baseData.setText(map.get(DICT_LABEL).toString());
                baseData.setValue(map.get(DICT_VALUE));
                baseDataList.add(baseData);
            }
            return baseDataList;
        } catch (Exception e) {
            log.error("取数据字典异常", e);
            return Lists.newArrayListWithExpectedSize(0);
        }
    }

    public List<String> getFasValues(String dictType){
        List<BaseData> dictionaryByTypeList = getDictionaryByType(FAS_SYS_DICT, dictType);
        if(CollectionUtils.isEmpty(dictionaryByTypeList)){
           return Collections.EMPTY_LIST;
        }
        List<String> collect = dictionaryByTypeList.stream().map(BaseData::getValue).map(Object::toString).collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取需要展示子品牌的品牌部简称
     * @return
     */
    public String getNameShortShowChildBrandName(){
        List<String> list = new ArrayList<>();
        list.add("PLACEHOLDER_WORD");
        List<String> fasValues = getFasValues(ACS_BRAND);
        list.addAll(fasValues);
        log.info("getNameShortShowChildBrandName:"+fasValues.toString());
        if(!CollectionUtils.isEmpty(list)){
            return StrUtil.toString(list.toArray());
        }
        return "PLACEHOLDER_WORD";
    }

    public List<String> getNameShortShowChildBrandNameList(){
        List<String> list = new ArrayList<>();
        list.add("PLACEHOLDER_WORD");
        List<String> fasValues = getFasValues(ACS_BRAND);
        return fasValues;
    }

    public String toBeDeletedScheduleIdInQueryCondition(){
        String s = toBeDeletedScheduleId();
        return FasUtil.formatInQueryCondition(s);
    }

    /**
     * 获取需要展示子品牌的品牌部简称
     * @return
     */
    public String toBeDeletedScheduleId(){
        List<String> list = new ArrayList<>();
        list.add("PLACEHOLDER_WORD");
        List<String> fasValues = getFasValues(ACS_TO_BE_DELETED_SCHEDULE_ID);
        list.addAll(fasValues);
        log.info("toBeDeletedScheduleId:"+fasValues.toString());
        if(!CollectionUtils.isEmpty(list)){
            return StrUtil.toString(list.toArray());
        }
        return "PLACEHOLDER_WORD";
    }

    public List<String> toBeDeletedScheduleIdList(){
        List<String> list = new ArrayList<>();
        list.add("PLACEHOLDER_WORD");
        List<String> fasValues = getFasValues(ACS_TO_BE_DELETED_SCHEDULE_ID);
        return fasValues;
    }

    public void setSysDictParam(Map<String, Object> model) {
        String nameShort = getNameShortShowChildBrandName();
        if(StringUtils.isNotEmpty(nameShort)){
            model.put("nameShort",nameShort);
        }
        FasUtil.formatInQueryCondition(model,"nameShort");
    }
}
